package com.acme.iolab.serialization;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ObjectServer {

    public static void main(String[] args) throws IOException {

        try (ServerSocket serverSocket = new ServerSocket(ConnectionSettings.SERVER_PORT);
             Socket socket = serverSocket.accept();
             ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream())) {

            List<Object> list = new ArrayList<>();
            for (int i = 0; i < ConnectionSettings.OBJECTS_COUNT; i++) {
                Object object = objectInputStream.readObject();
                list.add(ConnectionSettings.SERIALIZER.deSerialize(object));
            }
            objectOutputStream.writeObject("finished: " + list.size());

            Object object = objectInputStream.readObject();
            list.add(ConnectionSettings.SERIALIZER.deSerialize(object));
            objectOutputStream.writeObject("finished: " + list.size());


        } catch (ClassNotFoundException e) {
            System.out.println("Error receiving files: " + e.getMessage());
            e.printStackTrace();
        }

    }

}
